package cn.itcast

/*
date:2022.03.16
author:dong hui
 */

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object WordCount {
  def main(args: Array[String]): Unit = {
    val sparkconf = new SparkConf().setAppName("WordCount").setMaster("local[2]")
    val sparkContext = new SparkContext(sparkconf)
    val data : RDD[String] = sparkContext.textFile("D:\\word\\words.txt")
    val words :RDD[String] = data.flatMap(_.split(" "))
    val wordAndOne :RDD[(String, Int)] = words.map(x =>(x,1))
    val result: RDD[(String, Int)] = wordAndOne.reduceByKey(_+_)
    val finalResult: Array[(String, Int)] = result.collect()
    println(finalResult.toBuffer)
    sparkContext.stop()
  }
}
